import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.Structure;
import com.sun.jna.win32.StdCallLibrary;

import java.util.List;

@SuppressWarnings("ALL")
public class Macitcsi3 {


    public interface CLibrary extends StdCallLibrary {


        interface FileReceivedCallbackCallback extends StdCallCallback  {
            int fileReceived(int siteId, String fileName);
        }

        interface GetTmpFileNameCallback extends StdCallCallback  {
            int getTmpFileName(int deviceId, String filename, int nameLength);
        }

        interface GetCommunicationConfigCallback extends StdCallCallback  {
            void getCommunicationConfig(int siteId, Pointer pointer);
        }

        void SetFileReceived(FileReceivedCallbackCallback fileReceivedCallbackCallback);

        void SetGetTmpFileName(GetTmpFileNameCallback getTmpFileNameCallback);

        void SetGetCommunicationConfig(GetCommunicationConfigCallback getCommunicationConfigCallback);

        void InitializeCSILibrary();

        boolean CreateCSIInstance(int deviceId, boolean bSquelchLogging);


        public static class CommunicationConfig extends Structure {

            public CommunicationConfig(Pointer p){
                super(p);
                read();
            }
            public static final List<String> FIELDS = createFieldsOrder("szComputerName", "modemtype", "baudrate", "function","port","parity","stopBits"
                    ,"dataBits","flowControl","DCProtocol","faxFlowControl","DSRTimeout","CTSTimeout","initString","AllowTempDevice","AllowAllDevice",
                    "szNetworkTarget","NetworkPort","RetryInterval");

            public byte[] szComputerName = new byte[16];

            public byte modemtype;


            public int baudrate;

            public byte function;

            public byte port;

            public byte parity;

            public byte stopBits;

            public byte dataBits;

            public byte flowControl;

            public byte DCProtocol;

            public byte faxFlowControl;

            public short DSRTimeout;

            public short CTSTimeout;

            public byte[] initString = new byte[256];

            public int AllowTempDevice;

            public int AllowAllDevice;

            public byte[] szNetworkTarget = new byte[65];

            public int NetworkPort;

            public int RetryInterval;


            @Override
            protected List getFieldOrder() {


                return FIELDS;
            }
        }

    }


    public static void main(String[] args)  {

        CLibrary lib = (CLibrary) Native.loadLibrary("C:\\workspace\\angel-bo\\angel-common\\src\\test\\java\\macitcsi.dll", CLibrary.class);

        CLibrary.FileReceivedCallbackCallback fileReceivedCallbackCallback = new CLibrary.FileReceivedCallbackCallback() {
            @Override
            public int fileReceived(int siteId, String fileName) {
                System.out.println("fileReceived");
                return 0;
            }
        };

        CLibrary.GetTmpFileNameCallback getTmpFileNameCallback = new CLibrary.GetTmpFileNameCallback() {
            @Override
            public int getTmpFileName(int deviceId, String filename, int nameLength) {
                System.out.println("getTmpFileName");
                return 0;
            }
        };

        CLibrary.GetCommunicationConfigCallback getCommunicationConfigCallback = new CLibrary.GetCommunicationConfigCallback() {
            @Override
            public void getCommunicationConfig(int siteId, Pointer pointer) {

                CLibrary.CommunicationConfig cfg = new CLibrary.CommunicationConfig(pointer);
                //CommunicationConfig cfg = new CommunicationConfig(pointer);
                //translate

                if (siteId < 5) {
                    cfg.szNetworkTarget = "127.0.0.1".getBytes();
                    cfg.modemtype = 4;
                    cfg.NetworkPort = 3000 + siteId;
                    cfg.RetryInterval = siteId;

                } else {
                    cfg.modemtype = 3;
                    cfg.port =4;
                    cfg.baudrate =57600;
                    cfg.parity= 0;
                    cfg.stopBits = 0;
                    cfg.flowControl = 0;
                }
                //cfg.write();
                System.out.println("getCommunicationConfig"+pointer);

            }
        };

        lib.SetFileReceived(fileReceivedCallbackCallback);
        lib.SetGetTmpFileName(getTmpFileNameCallback);
        lib.SetGetCommunicationConfig(getCommunicationConfigCallback);

        lib.InitializeCSILibrary();
        try{
            lib.CreateCSIInstance(6, false);
            Thread.sleep(100000);
        }catch (Exception e){
            e.printStackTrace();
        }


    }
}





